package uk.co.hephistocles.blobGame;

public class Line {
	public int x1;
	public int y1;
	public int x2;
	public int y2;
	public double normal;
	
	public Line(int x, int y, int a, int b, double normal) {
		x1 = x;
		y1 = y;
		x2 = a;
		y2 = b;
		this.normal = normal;
	}
	
	public Line(float x, float y, float a, float b, double normal) {
		x1 = (int) x;
		y1 = (int)y;
		x2 = (int)a;
		y2 = (int)b;
		this.normal = normal;
	}
	
	public Line(float x, float y, float a, float b) {
		x1 = (int) x;
		y1 = (int)y;
		x2 = (int)a;
		y2 = (int)b;
		normal = Math.atan2(y-b,x-a);
	}
	
	public boolean intersects(Line b) {
		//check parallel
		float numerator = (b.y2 - b.y1)*(x2-x1) - (b.x2-b.x1)*(y2-y1);
		if (numerator == 0) 
			return false;
		float t1 = ((b.x2 - b.x1)*(y1-b.y1) - (b.y2 - b.y1)*(x1-b.x1));
		float t2 = ((x2 - x1)*(y1-b.y1) - (y2 - y1)*(x1-b.x1));
		//check coincident
		if ((t1==0) && (t2 ==0)) 
			return true;
		//check that the intersection occurs within line segment
		if ((t1<0) || (t1>numerator))
			return false;
		if ((t2<0) || (t2>numerator))
			return false;
		
		//otherwise, intersectino occurs
		return true;
	}

	public String toString() {
		return "Line from " + x1 + ", " + y1 + " to " + x2 + ", " + y2;
	}
}
